#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;
const int MODX = 39989;
const int MODY = 1e9;
const int MX = 40005;
const double eps = 1e-9;

int n, tot, lastans;
struct Line {
  double k, b;
} lines[N];
Line get(int x1, int y1, int x2, int y2) {
  if (x1 == x2) return Line{0, (double)max(y1, y2)};
  double k = (y2 - y1) * 1.0 / (x2 - x1);
  double b = y1 - k * x1;
  return Line{k, b};
}
double f(int i, int x) { return lines[i].k * x + lines[i].b; }
int cmp(int i, int j, int x) {
  double y1 = f(i, x);
  double y2 = f(j, x);
  // cout << x << ' ' << i << ' ' << j << ' ' << y1 << ' ' << y2 << endl;
  if (abs(y1 - y2) < eps)
    return 0;
  else if (y1 > y2)
    return 1;
  else
    return -1;
}
int tr[MX << 2];
void change(int id, int L, int R, int p, int l, int r) {
  int mid = (l + r) >> 1;
  if (L <= l && R >= r) {
    int res = cmp(id, tr[p], mid);
    if (res == 1 || (!res && id < tr[p])) swap(id, tr[p]);
    res = cmp(id, tr[p], l);
    if (res == 1 || (!res && id < tr[p])) change(id, L, R, p << 1, l, mid);
    res = cmp(id, tr[p], r);
    if (res == 1 || (!res && id < tr[p]))
      change(id, L, R, p << 1 | 1, mid + 1, r);
    return;
  }
  if (L <= mid) change(id, L, R, p << 1, l, mid);
  if (R > mid) change(id, L, R, p << 1 | 1, mid + 1, r);
}
int query(int x, int p, int l, int r) {
  if (l == r) return tr[p];
  int mid = (l + r) >> 1;
  int id =
      x <= mid ? query(x, p << 1, l, mid) : query(x, p << 1 | 1, mid + 1, r);
  int res = cmp(tr[p], id, x);
  if (res == 1)
    return tr[p];
  else if (res == -1)
    return id;
  else
    return min(tr[p], id);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  while (n--) {
    int op;
    cin >> op;
    if (op == 1) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      x1 = (x1 + lastans - 1) % MODX + 1;
      x2 = (x2 + lastans - 1) % MODX + 1;
      y1 = (y1 + lastans - 1) % MODY + 1;
      y2 = (y2 + lastans - 1) % MODY + 1;
      lines[++tot] = get(x1, y1, x2, y2);
      if (x1 > x2) swap(x1, x2);
      change(tot, x1, x2, 1, 1, MODX);
    } else {
      int x;
      cin >> x;
      x = (x + lastans - 1) % MODX + 1;
      lastans = query(x, 1, 1, MODX);
      cout << lastans << endl;
    }
  }
  return 0;
}